package org.managed.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;


import org.model.Maestro;
import org.service.IMaestroService;
import org.springframework.dao.CleanupFailureDataAccessException;


@ManagedBean(name = "data")
@SessionScoped
public class PruebaBean implements Serializable {
	
	private static final long serialVersionUID = 1L;
	
	
	// Spring User Service is injected...
	@ManagedProperty(value = "#{maestroService}")
	IMaestroService maestroService;
	public PruebaBean() {
		System.out.println("entro");
	}
	
	private Maestro maestro = new Maestro();
	
    @SuppressWarnings("rawtypes")  
	private DataModel motivo; 
	List<Maestro> maestroList;
	
	public void deleteFamilia(){
		Maestro m = (Maestro)motivo.getRowData();				
		maestroService.deleteMaestro(m);
	}

	
    @SuppressWarnings({ "rawtypes", "unchecked" }) 
	public DataModel getMotivos() {
		System.out.println("getMotivo");
		List<Maestro> lista = maestroService.getMaestros();
    	motivo = new ListDataModel(lista);
    	return motivo;
	}

	
	public void reset(ActionEvent event) {
		maestro.setId(0);
		maestro.setNombre("");
		maestro.setDescripcion("");
		maestro.setFamilia("");
	}
	
    public void prepararEditarMaestro(ActionEvent actionEvent){
    	maestro = (Maestro)(motivo.getRowData());
    }
    
    public void editarMaestro(ActionEvent actionEvent){	
	    maestroService.updateMaestro(maestro);
    }
    
    public void prepararAdicionarMaestro(ActionEvent actionEvent){
    maestro = new Maestro();
    }
    
	public void adicionarMaestro(ActionEvent event) {
		
		if (maestro.getId() != 0) {
		    maestroService.updateMaestro(maestro);
		    
	   } else {
			maestroService.addMaestro(maestro);
		}
		reset(event);
	}

	public void setMaestroList(List<Maestro> maestroList) {
		this.maestroList = maestroList;
	}

	public IMaestroService getMaestroService() {
		return maestroService;
	}

	public void setMaestroService(IMaestroService maestroService) {
		this.maestroService = maestroService;
	}


	public Maestro getMaestro() {
		return maestro;
	}

	public void setMaestro(Maestro maestro) {
		this.maestro = maestro;
	}
    @SuppressWarnings({ "rawtypes" }) 
	public void setMotivos(DataModel motivo) {
		this.motivo = motivo;
	}
	
	

	
}